package com.perfect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.perfect.mapper.StockMapper;
import com.perfect.model.Stock;
import com.perfect.model.StockExample;
import com.perfect.service.StockService;
import io.netty.util.internal.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper,Stock> implements StockService {

    @Autowired
    private StockMapper stockMapper;



    @Override
    public void reduct(Long id) {

        LambdaQueryWrapper<Stock> lambda = new QueryWrapper<Stock>().lambda();
        lambda.eq(Stock::getId, id);
        Stock stock = stockMapper.selectOne(lambda);
        System.out.println("库存扣减前，数量："+stock.getTotal());
       if(ObjectUtils.isNotNull(stock)){
           LambdaUpdateWrapper<Stock> update = new UpdateWrapper<Stock>().lambda();
           update.eq(Stock::getId,stock.getId());
           stock.setTotal(stock.getTotal()-1);
           stockMapper.update(stock,update);
           System.out.println("库存扣减后的数量:"+stock.getTotal());

       }
    }

    @Override
    public List<Stock> queryList(){
       return  stockMapper.selectList(new QueryWrapper<>());
    }
}
